package com.yimidida.library.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;

import com.yimidida.library.base.ICallBackResultListener;


/**
 * 作者：suichengju
 * 时间：2023/7/28:19:00
 * 邮箱：325927775@qq.com
 * 说明：
 */
public class ProgressDialogUtils {
    private static final String TAG = "ProgressDialogUtils"; // 设置显示时间为3秒
    private static ICallBackResultListener respCallBackListener;
    private static int displayTime = 3000; // 设置显示时间为3秒
    private static Handler handler = new Handler();
    private static ProgressDialog progressDialog;//ProgressDialog这个对象你从上面的导入也可以看到，这是Android库自带的

    public static void showProgressDialog(Context context, String message) {
        try {
            progressDialog = new ProgressDialog(context);
            NavigationBarUtil.focusNotAle(progressDialog.getWindow());
            progressDialog.setMessage(message);
            progressDialog.setCancelable(false);
            progressDialog.show();
            NavigationBarUtil.hideNavigationBar(progressDialog.getWindow());
            NavigationBarUtil.clearFocusNotAle(progressDialog.getWindow());
        } catch (Exception e) {

        }

    }

    public static void showProgressDialog(Context context, String message, int dismissTime) {
        try {
            displayTime = dismissTime;
            progressDialog = new ProgressDialog(context);
            NavigationBarUtil.focusNotAle(progressDialog.getWindow());
            progressDialog.setMessage(message);
            progressDialog.setCancelable(false);
            progressDialog.show();
            NavigationBarUtil.hideNavigationBar(progressDialog.getWindow());
            NavigationBarUtil.clearFocusNotAle(progressDialog.getWindow());
            handler.postDelayed(() -> {
                if (progressDialog != null && progressDialog.isShowing()) {
                    progressDialog.dismiss(); // 关闭Dialog
                    progressDialog=null;
                }
            }, displayTime);
        } catch (Exception e) {

        }
    }

    public static void showProgressDialog(Context context, String message, int dismissTime, ICallBackResultListener respCallBackListener) {
        try {
            displayTime = dismissTime;
            progressDialog = new ProgressDialog(context);
            NavigationBarUtil.focusNotAle(progressDialog.getWindow());
            progressDialog.setMessage(message);
            progressDialog.setCancelable(false);
            progressDialog.show();
            NavigationBarUtil.hideNavigationBar(progressDialog.getWindow());
            NavigationBarUtil.clearFocusNotAle(progressDialog.getWindow());
            handler.postDelayed(() -> {
                if (respCallBackListener != null) {
                    respCallBackListener.onCallBack(null);
                }
                hideProgressDialog();
            }, displayTime);
        } catch (Exception e) {
        }

    }



    public static void hideProgressDialog() {
        handler.removeCallbacksAndMessages(null);
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
        respCallBackListener = null;
        progressDialog = null;
    }

}